package demo4.controller;

import demo4.mapper.IUserMapper;
import demo4.mapper.UserMapper;
import demo4.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Scanner;

@Component
public class UserController {
    @Autowired
    private IUserMapper userMapper;

    public void start(){
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入编号：");
        System.out.println("1.用户注册");
        System.out.println("2.用户登录");
        System.out.println("3.用户列表");
        System.out.println("4.修改用户信息");
        int num = sc.nextInt();
        switch (num){
            case 1:
                userReg();
                break;
            case 2:
                userLogin();
                 break;
            case 3:
                 userList();
                 break;
            case 4:
                  userUpdate();
                 break;
        }
    }


    private void userReg(){
        System.out.println("欢迎用户注册");
        Scanner sc = new Scanner(System.in);
        User user = new User();
        System.out.println("请输入用户名：");
        user.setUsername(sc.next());
        System.out.println("请输入密码：");
        user.setPassword(sc.next());
        System.out.println("请输入昵称：");
        user.setNickname(sc.next());
        System.out.println("请输入年龄：");
        user.setAge(sc.nextInt());
        System.out.println(  user);
        //获取信息后进行验证
        if(user.getUsername() == null || user.getUsername().equals("")){
            System.out.println("用户名不能为空！");
            return;
        }
        if(user.getPassword() == null || user.getPassword().equals("")){
            System.out.println("密码不能为空！");
            return;
        }
        if(user.getNickname() == null || user.getNickname().equals("")){
            System.out.println("昵称不能为空！");
            return;
        }
        if(user.getAge() <= 0){
            System.out.println("年龄不能小于0！");
            return;
        }
        if(userMapper.selectByUserName(user.getUsername()) != null ){
            System.out.println("用户已存在！");
            return;
        }

        //验证通过后，将用户信息插入数据库中
        int result = userMapper.insert(user);
        //验证结果
        System.out.println(result > 0?"注册成功！":"注册失败！");

    }


    private void userLogin(){
        User user = new User();
        System.out.println("用户登录");
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入用户名：");
        user.setUsername(sc.next());
        System.out.println("请输入密码：");
        user.setPassword(sc.next());
        //验证信息是否合法

        //验证通过后，用户信息查询
        User user1 = userMapper.selectByUserName(user.getUsername());
        if(user1 != null ){
           if(user1.getPassword().equals(user.getPassword())){
               System.out.println("登录成功！");
           }else{
               System.out.println("密码错误！");
           }
        }else{
            System.out.println("用户不存在！");
        }

    }



    private void userList(){
        List<User> list = userMapper.selectAll();
        for (User user : list) {
            System.out.println(user);
        }
    }



    private void userUpdate(){
        System.out.println("用户修改");
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入用户名：");
        String username = sc.next();
        //输入是否合法

        //验证通过后，用户信息查询
        User user = userMapper.selectByUserName(username);
        if(user != null ){
            System.out.println("请输入昵称：");
            user.setNickname(sc.next());
            System.out.println("请输入年龄：");
            user.setAge(sc.nextInt());
            int result = userMapper.userUpdate(user);
            //输入验证

            //验证通过后，用户信息修改
            System.out.println(result > 0?"修改成功！":"修改失败！");
        }else{
            System.out.println("用户不存在！");
        }
    }

}
